package com.neusmart.common.util;

import android.content.Context;
import android.os.Environment;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ResourceUtil {

    /**
     * 从Raw文件中读取
     *
     * @param context
     * @param rawResource
     * @return
     */
    public static String getFromRaw(Context context, int rawResource) {
        try {
            InputStreamReader inputReader = new InputStreamReader(context
                    .getResources().openRawResource(rawResource));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String Result = "";
            while ((line = bufReader.readLine()) != null)
                Result += line;
            return Result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 从assets读取
     *
     * @param context
     * @param fileName
     * @return
     */
    public static String getFromAssets(Context context, String fileName) {
        try {
            InputStreamReader inputReader = new InputStreamReader(context
                    .getResources().getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String result = "";
            while ((line = bufReader.readLine()) != null)
                result += line;
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 从Sdcard文件中读取
     *
     * @param path
     * @return
     */
    public static String readFromSdcard(String path) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    path)));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null)
                sb.append(line);
            br.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 写入内容到Sdcard文件
     *
     * @param path
     * @param content
     */
    public static void writeToSdcard(String path, String content) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(path);
            fos.write(content.getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文件夹
     *
     * @param folderPath 文件夹完整绝对路径
     */
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); // 删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            File myFilePath = new File(filePath);
            myFilePath.delete(); // 删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除指定文件夹下所有文件
     *
     * @param path 文件夹完整绝对路径
     * @return
     */
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists())
            return flag;
        if (!file.isDirectory())
            return flag;
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator))
                temp = new File(path + tempList[i]);
            else
                temp = new File(path + File.separator + tempList[i]);
            if (temp.isFile())
                temp.delete();
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);// 再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }

    public static void copyFromAsset2SDCard(Context context, String filename, String toFolder) throws IOException {
        File folder = new File(toFolder);
        if (!folder.exists())
            folder.mkdirs();
        File file = new File(toFolder, filename);
        if (!sdCardAvailable() || file.exists())
            return;
        FileOutputStream fos = new FileOutputStream(file);
        InputStream is = context.getResources().getAssets().open(filename);
        byte[] buffer = new byte[1024];
        int count = 0;
        while ((count = is.read(buffer)) > 0)
            fos.write(buffer, 0, count);
        fos.close();
        is.close();
    }

    public static boolean sdCardAvailable() {
        return Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState());
    }

}
